using UnityEngine;

internal class BGMPlayer
{
	private enum BGMFocus
	{
		First,
		Second
	}

	private const float m_TransitTime = 0.5f;

	private AudioSource[] m_BGMs;

	private BGMFocus m_focus;

	private BGMFocus m_reverse => (m_focus == BGMFocus.First) ? BGMFocus.Second : BGMFocus.First;

	public BGMPlayer(GameObject o)
	{
		m_BGMs = new AudioSource[2];
		for (int i = 0; i < 2; i++)
		{
			m_BGMs[i] = SupportTools.AddChild<AudioSource>(o);
			m_BGMs[i].name = "BGM_" + (i + 1);
			m_BGMs[i].loop = true;
			m_BGMs[i].playOnAwake = false;
			m_BGMs[i].volume = (AudioManager.Sound ? 1 : 0);
			m_BGMs[i].pitch = 1f;
		}
		m_focus = BGMFocus.Second;
		m_BGMs[1].volume = 0f;
	}

	public void DestroySelf()
	{
		for (int i = 0; i < 2; i++)
		{
			m_BGMs[i].clip = null;
		}
	}

	public void PlayBGM(string bgmName, bool stopImmediate)
	{
		int num = 0;
		if (stopImmediate)
		{
			m_BGMs[(int)m_focus].Stop();
		}
		else
		{
			AudioTo(m_BGMs[(int)m_focus], 0f);
			num = 1;
		}
		if (m_BGMs[(int)m_reverse].clip == null || m_BGMs[(int)m_reverse].clip.name.CompareTo(bgmName) != 0)
		{
			AudioClip clip = Resources.Load("Audio/" + bgmName) as AudioClip;
			m_BGMs[(int)m_reverse].clip = clip;
		}
		m_BGMs[(int)m_reverse].PlayDelayed((float)(double)(ulong)num);
		if (AudioManager.Sound)
		{
			AudioTo(m_BGMs[(int)m_reverse], 1f);
		}
		m_focus = m_reverse;
	}

	public void CloseBGM(bool closeImmediate)
	{
		for (int i = 0; i < 2; i++)
		{
			iTween[] components = m_BGMs[i].GetComponents<iTween>();
			for (int j = 0; j < components.Length; j++)
			{
				UnityEngine.Object.Destroy(components[j]);
			}
			if (closeImmediate)
			{
				m_BGMs[i].volume = 0f;
			}
			else
			{
				AudioTo(m_BGMs[i], 0f);
			}
		}
	}

	public void OpenBGM()
	{
		iTween[] components = m_BGMs[(int)m_focus].GetComponents<iTween>();
		for (int i = 0; i < components.Length; i++)
		{
			UnityEngine.Object.Destroy(components[i]);
		}
		AudioTo(m_BGMs[(int)m_focus], 1f);
	}

	private void AudioTo(AudioSource audioSource, float volume)
	{
		iTween.AudioTo(audioSource.gameObject, iTween.Hash("volume", volume, "time", 0.5f, "ignoretimescale", true));
	}
}
